// Copyright (c) 2009-2011, chunquedong
//
// This file is part of ChunMap project
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
//
// History:
//   2011-10-30  Jed Young  Creation
//

using [java]com.vividsolutions.jts.io
using [java]com.vividsolutions.jts.geom::Geometry as JGeometry
using [java]fanx.interop

using chunmapModel

class Jts
{
  WKBWriter writer := WKBWriter()
  WKBReader reader := WKBReader()

  Geometry toCm(JGeometry jg)
  {
    Buf buf := Buf()
    writer.write(jg, OutputStreamOutStream(Interop.toJava(buf.out)))
    buf.flip
    return Geometry.fromStream(buf.in)
  }

  JGeometry toJts(Geometry g)
  {
    Buf buf := Buf()
    g.save(buf.out)
    buf.flip
    return reader.read(InputStreamInStream(Interop.toJava(buf.in)))
  }

  **
  ** Computes the smallest convex Polygon that contains all the points in the Geometry.
  **
  Geometry convexHull(Geometry g)
  {
    toCm(toJts(g).convexHull)
  }

  **
  ** Tests whether this geometry contains the argument geometry.
  **
  Bool contains(Geometry g1, Geometry g2)
  {
    toJts(g1).contains(toJts(g2))
  }

  **
  ** Tests whether this geometry is covered by the argument geometry.
  **
  Bool coveredBy(Geometry g1, Geometry g2)
  {
    toJts(g1).coveredBy(toJts(g2))
  }

  **
  ** Tests whether this geometry covers the argument geometry.
  **
  Bool covers(Geometry g1, Geometry g2)
  {
    toJts(g1).covers(toJts(g2))
  }

  **
  ** Tests whether this geometry crosses the argument geometry.
  **
  Bool crosses(Geometry g1, Geometry g2)
  {
    toJts(g1).crosses(toJts(g2))
  }

  **
  ** Tests whether this geometry is disjoint from the argument geometry.
  **
  Bool disjoint(Geometry g1, Geometry g2)
  {
    toJts(g1).disjoint(toJts(g2))
  }

  **
  ** Tests whether this geometry is topologically equal to the argument geometry.
  **
  Bool equals2(Geometry g1, Geometry g2)
  {
    toJts(g1).equals(toJts(g2))
  }

  **
  ** Tests whether two geometries are exactly equal in their normalized forms.
  **
  Bool equalsNorm(Geometry g1, Geometry g2)
  {
    toJts(g1).equalsNorm(toJts(g2))
  }

  **
  ** Tests whether this geometry is topologically equal to the argument geometry as defined by the SFS equals predicate.
  **
  Bool equalsTopo(Geometry g1, Geometry g2)
  {
    toJts(g1).equalsTopo(toJts(g2))
  }

  **
  ** Tests whether this geometry intersects the argument geometry.
  **
  Bool intersects(Geometry g1, Geometry g2)
  {
    toJts(g1).intersects(toJts(g2))
  }

  **
  ** Tests whether this geometry touches the argument geometry.
  **
  Bool touches(Geometry g1, Geometry g2)
  {
    toJts(g1).touches(toJts(g2))
  }

  **
  ** Tests whether this geometry is within the specified geometry.
  **
  Bool within(Geometry g1, Geometry g2)
  {
    toJts(g1).within(toJts(g2))
  }

  **
  ** Returns the minimum distance between this Geometry and the Geometry g.
  **
  Float distance(Geometry g1, Geometry g2)
  {
    toJts(g1).distance(toJts(g2))
  }

  **
  ** Computes a buffer area around this geometry having the given width.
  **
  Geometry buffer(Geometry g, Float distance)
  {
    toCm(toJts(g).buffer(distance))
  }
}